package com.ruoyi.common.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

public class SmsUtils {
    private static SmsUtils instance;
    public static final String url = "http://web.cr6868.com/asmx/smsservice.aspx";
    private SmsUtils() {
    }

    /**
     * 单例方法
     */
    public static SmsUtils getInstance() {
        if (instance == null) {
            instance = new SmsUtils();
        }
        return instance;
    }

    /**
     * DOM4J XML 解析
     */
    public String readXML() {

        return null;
    }

    /**
     * 短信单发 tel ： 号码 msg 内容
     */
    public Boolean send(String tel, String msg) {
        try {
            msg = "您本次操作验证码："+ msg;
//			httpSend(tel, msg);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 短信群发 tel ：号码集合 msg 发送内容
     */
    public Boolean send(List<String> tel, String msg) {
        String str1 = tel.toString().replace("[", "");
        String str2 = str1.replace("]", "");
        return true;
    }

    public String smsCode(String tel,String code)
            throws Exception {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        // 目标地址
        HttpPost httppost = new HttpPost(url);
        System.out.println("请求: " + httppost.getRequestLine());
        // post 参数 传递
        List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();

        nvps.add(new BasicNameValuePair("name", "18611583050")); // 参数
        nvps.add(new BasicNameValuePair("pwd", "2FC15D188FF2FF040BE53F0F4BA5")); // 参数
        nvps.add(new BasicNameValuePair("content", "so：单号（"+code+"）,金额（"+code+"）此信息勿回！【郑州普易森】")); // 参数
        nvps.add(new BasicNameValuePair("mobile", tel)); // 参数
        nvps.add(new BasicNameValuePair("stime", "")); // 参数
        nvps.add(new BasicNameValuePair("type", "pt")); // 参数
        nvps.add(new BasicNameValuePair("sign", "")); // 参数
        nvps.add(new BasicNameValuePair("extno", "")); // 参数
        httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); // 设置参数给Post

        // 执行
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        System.out.println(response.getStatusLine());
        if (entity != null) {
            System.out.println("Response content length:"
                    + entity.getContentLength());
        }
        // 显示结果
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                entity.getContent(), "UTF-8"));

        String line = null;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        if (entity != null) {
            entity.consumeContent();
        }
        return null;
    }

    public String smsOrders(String tel, String orderNo,String amount)
            throws Exception {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        // 目标地址
        HttpPost httppost = new HttpPost(url);
        System.out.println("请求: " + httppost.getRequestLine());
        // post 参数 传递
        List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();

        nvps.add(new BasicNameValuePair("name", "18611583050")); // 参数
        nvps.add(new BasicNameValuePair("pwd", "2FC15D188FF2FF040BE53F0F4BA5")); // 参数
        nvps.add(new BasicNameValuePair("content", "so：单号（"+orderNo+"）,金额（"+amount+"）此信息勿回！【郑州普易森】")); // 参数
        nvps.add(new BasicNameValuePair("mobile", tel)); // 参数
        nvps.add(new BasicNameValuePair("stime", "")); // 参数
        nvps.add(new BasicNameValuePair("type", "pt")); // 参数
        nvps.add(new BasicNameValuePair("sign", "")); // 参数
        nvps.add(new BasicNameValuePair("extno", "")); // 参数
        httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); // 设置参数给Post

        // 执行
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        System.out.println(response.getStatusLine());
        if (entity != null) {
            System.out.println("Response content length:"
                    + entity.getContentLength());
        }
        // 显示结果
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                entity.getContent(), "UTF-8"));

        String line = null;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        if (entity != null) {
            entity.consumeContent();
        }
        return null;
    }


    public String smsRegister(String tel, String name, String ip,String email)
            throws Exception {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        // 目标地址
        HttpPost httppost = new HttpPost(url);
        System.out.println("请求: " + httppost.getRequestLine());
        // post 参数 传递
        List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
        new Date();
        nvps.add(new BasicNameValuePair("name", "18611583050")); // 参数
        nvps.add(new BasicNameValuePair("pwd", "2FC15D188FF2FF040BE53F0F4BA5")); // 参数
        nvps.add(new BasicNameValuePair("content", "so："+name+",当前IP（"+ip+"）,邮箱（"+email+"）此信息勿回！【郑州普易森】")); // 参数
        nvps.add(new BasicNameValuePair("mobile", tel)); // 参数
        nvps.add(new BasicNameValuePair("stime", "")); // 参数
        nvps.add(new BasicNameValuePair("type", "pt")); // 参数
        nvps.add(new BasicNameValuePair("sign", "")); // 参数
        nvps.add(new BasicNameValuePair("extno", "")); // 参数
        httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); // 设置参数给Post

        // 执行
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        System.out.println(response.getStatusLine());
        if (entity != null) {
            System.out.println("Response content length:"
                    + entity.getContentLength());
        }
        // 显示结果
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                entity.getContent(), "UTF-8"));

        String line = null;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        if (entity != null) {
            entity.consumeContent();
        }
        return null;
    }

    public static void main(String[] args) throws Exception {
//		SmsUtils.getInstance().send("18611583050", "654321");
//		SmsUtils.getInstance().httpSend("18611583050", getBindNum());
//		SmsUtils.getInstance().httpSendMethod("http://192.168.1.120:8181/interface/UI?transCode=L0006T&userCd=3065ff7f52ff4fc1baaa02076c3ecbd2&orderCd=3065ff7f52ff4fc1baaa02076c3ecbd2&orderDesc=神仙果&realPrice=7000.0&totalPrice=8000.0");
//		SmsUtils.getInstance().smsOrders("18611583050", "PI10101013", "128.68");
        SmsUtils.getInstance().smsRegister("18611583050", "赵海深", "192.168.0.1", "zhaohaishen-2005@163.com");
    }

    public String httpSendMethod(String url)
            throws Exception {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        // 目标地址
        String[] aa = url.split("\\?");
        HttpPost httppost = new HttpPost(aa[0]);
        System.out.println("请求: " + httppost.getRequestLine());
        String paramStr = aa[1];
        // post 参数 传递
        List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
        //中盟教育
        String[] array = paramStr.split("&");
        for(int i=0;i<array.length;i++){
            String[] s = array[i].split("=");
            nvps.add(new BasicNameValuePair(s[0], s[1])); // 参数
        }
        httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); // 设置参数给Post
        // 执行
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        System.out.println(response.getStatusLine());
        if (entity != null) {
            System.out.println("Response content length:"
                    + entity.getContentLength());
        }
        // 显示结果
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                entity.getContent(), "UTF-8"));

        String line = null;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        if (entity != null) {
            entity.consumeContent();
        }
        return null;
    }

    /**
     * 随机生成4位验证码
     */
    public static String getBindNum() {
        String[] beforeShuffle = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
        List<String> list = Arrays.asList(beforeShuffle);
        Collections.shuffle(list);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i));
        }
        String afterShuffle = sb.toString();
        String result = afterShuffle.substring(3, 7);
        return result;
    }
}
